/**
 * Created by chensk740 on 2018/1/9.
 */
import {Component, OnInit, ViewChild} from '@angular/core';
import {ToolsService} from "@shared/service/tools/tools.service";
import {SeedInfiniteScroll} from "@shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component";
import {SharingService} from "./sharing.service";
import {Router} from "@angular/router";

@Component({
  selector: 'sharing',
  templateUrl: './sharing.component.html',
  styleUrls: ['./sharing.component.scss']
})
export class SharingComponent implements OnInit {

  public contentData= {
    espnCode: '' ,
    page: 1 ,
    row: 10
  };
  public hasData= true;
  public ifFirstTime= true; // 是否加载频道首页数据
  public sharingNavList = [];
  public sharingList = [];
  @ViewChild(SeedInfiniteScroll) infiniteScroll;

  constructor(private sharingserive: SharingService,
              private router: Router,
              public tools: ToolsService) {
  }

  /**
   * @description 获取频道列表
   * @author chensk740
   * @date 2018-1-11
   */
  getSharingNav() {
    this.sharingserive.getSharingNavList().subscribe((res) => {
      if (res.result === '0') {
        this.sharingNavList = res.data;
        this.contentData.espnCode = res.data[0].espnCode;
        this.getSharingContent();
      }
    });
  }

  /**
   * @description 获取频道内容列表
   * @param data 频道id
   * @author chensk740
   * @date 2018-1-11
   */
  getSharingContent() {
    this.tools.showLoading();
    // this.infiniteScroll.complete();
    this.sharingserive.getSharingContent(this.contentData).subscribe((res) => {
      this.infiniteScroll.complete();
      if (res.result === '0') {
        this.contentData.page += 1;
        if ( this.ifFirstTime) {
          this.sharingList = res.data.list;
          res.data.list.length === 0 ? this.hasData = false : this.hasData = true;
        }else {
          this.sharingList = [...this.sharingList, ...res.data.list];
        }
        if (!res.data.hasNextPage) {
          this.infiniteScroll.enable(false);
        }
        this.ifFirstTime = false;
      }
      this.tools.hideLoading();
    });
  }
  /**
   * @description 切换头部频道
   * @param code 频道分类编码
   * @author chensk740
   * @date 2018-1-11
   */
  navIndexclick(code) {
    this.contentData.espnCode = code;
    this.contentData.page = 1;
    this.ifFirstTime = true;
    this.getSharingContent();
  }
  /**
   * @description 频道内容翻页
   * @param code 频道分类编码
   * @author chensk740
   * @date 2018-1-11
   */
  onScroll(event) {
    this.getSharingContent();
  }


  /**
   * @description 跳转到详情
   * @param id 内容id
   * @author chensk740
   * @date 2018-1-11
   */
  goToTarget(article) {
    if (article.infoType === 2) {
      this.router.navigate(['/news/photo-news',  article.cateId, article.contentId]);
    } else {
      this.router.navigate(['/news',  article.contentId] );
    }
  }

  /**
   * @description 跳转到详情
   * @param id 内容id
   * @author chensk740
   * @date 2018-1-11
   */
  goToFeedDetail(feed) {
    this.router.navigate(['/feed/detail', feed.contentId]);
  }

  ngOnInit() {
    this.getSharingNav();
  }


}
